(function($) {
	$.fn.price_format = function (options){
		var defaults ={
			prefix:"R$ ",
			centsSeparator:",",
			thousandsSeparator:".",
			retrieve:function(obj){
				return obj.val();
			},
			apply:function(obj,value){
				obj.val(value);
			}
		};
		var options = $.extend(defaults, options);
		// format definitions
		var prefix = options.prefix;
		var centsSeparator = options.centsSeparator;
		var thousandsSeparator = options.thousandsSeparator;
		var formatted = '';
		var thousandsFormatted = '';
		var str = options.retrieve($(this));

		// skip everything that isn't a number
		// and skip left 0
		var isNumber = /[0-9]/;
		for (var i=0;i<(str.length);i++) {
			char = str.substr(i,1);
			if (formatted.length==0 && char==0) char = false;
			if (char && char.match(isNumber)) formatted = formatted+char;
		}

		// format to fill with zeros when < 100
		while (formatted.length<3) formatted = '0'+formatted;
		var centsVal = formatted.substr(formatted.length-2,2);
		var integerVal = formatted.substr(0,formatted.length-2);

		// apply cents pontuation
		formatted = integerVal+centsSeparator+centsVal;

		// apply thousands pontuation
		if (thousandsSeparator) {
			var thousandsCount = 0;
			for (var j=integerVal.length;j>0;j--) {
				char = integerVal.substr(j-1,1);
				thousandsCount++;
				if (thousandsCount%3==0) char = thousandsSeparator+char;
				thousandsFormatted = char+thousandsFormatted;
			}
			if (thousandsFormatted.substr(0,1)==thousandsSeparator) thousandsFormatted = thousandsFormatted.substring(1,thousandsFormatted.length);
			formatted = thousandsFormatted+centsSeparator+centsVal;
		}

		// apply the prefix
		if (prefix) formatted = prefix+formatted;

		// replace the value
		options.apply($(this),formatted);

	}
	$.fn.FcKeyPad = function(options) {
		var defaults = {
			img: ".sistema/icones/calculator.png"
		};
		if(!$('#FcKeyPad')[0]) $('body').append('<div id="FcKeyPad"/>');
		$('#FcKeyPad').hide();
		var options = $.extend(defaults, options);
		$(this).after($('<img src="'+options.img+'" class="img-calculator">'));
		disparar = function(){
				var instance = $(this).prev();
				$('#FcKeyPad').dialog(
					{
						minWidth:"250px",
						width:"250px",
						modal:true,
						open: function(event, ui) {
							$(this).prev().css({
								'text-align':'right',
							});
							$(this).prev().html('');
							$(this).prev().append('<span class="fc-teclado-numerico-valor">'+instance.val()+'</span>');
							$(".ui-dialog-titlebar-close").hide();
							$(this).html('');
							var keys=new Array(new Array(7,8,9),new Array(4,5,6),new Array(1,2,3));
							var keypad =
								'<div class="fc-teclado-numerico">'+
									'<div class="fc-teclado-numerico-keypad">'+
										'<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="1">';
							for(i in keys){
								keypad += '<tr>';
								for(j in keys[i]){
									keypad += '<td class="fc-teclado-numerico-digit">' + keys[i][j] + '</td>';
								}
								keypad += '</tr>';
							}
							keypad += '<tr>'+
								'<td class="fc-teclado-numerico-digit">0</td>'+
								'<td class="fc-teclado-numerico-digit">,</td>'+
								'<td class="fc-teclado-numerico-clear">AC</td>'+
								'</tr></table></div></div>';

							$(this).append(keypad);
							$('.fc-teclado-numerico-clear').click(function(){
								$('.fc-teclado-numerico-valor').html('R$ 0,00');
							});
							$('.fc-teclado-numerico-valor').price_format({
								apply:function(component,value){
									component.html(value);
								},
								retrieve:function(component){
									return component.html();
								}
							});
							$('.fc-teclado-numerico-digit').click(function(){
								$('.fc-teclado-numerico-valor').html($('.fc-teclado-numerico-valor').html()+$(this).html());
								$('.fc-teclado-numerico-valor').price_format({
									apply:function(component,value){
										component.html(value);
									},
									retrieve:function(component){
										return component.html();
									}
								});
							});
							$('.fc-teclado-numerico-digit, .fc-teclado-numerico-clear').css({
								"width": "30px",
								"height": "40px",
								"text-align": "center"
							});
						},
						buttons:{
							Ok:function(){
								instance.val($('.fc-teclado-numerico-valor').html());
								$('.fc-teclado-numerico-valor').html('0');
								$(this).dialog('close');
							},
							Cancelar:function(){
								$(this).dialog('close');
							}
						}
					}
				);
			}
		$(this).each(function(){
			$(this).next().bind('click', disparar);
		});
	}
})(jQuery);